package com.ruoyi.project.system.erp.controller;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.digest.Pwd;
import com.ruoyi.framework.config.URLConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.page.TableSupport;
import com.ruoyi.project.system.erp.entity.ContractResult;
import com.ruoyi.project.system.erp.entity.MaterialResult;
import com.ruoyi.project.system.erp.param.UpdateSchedule;
import com.ruoyi.project.system.erp.service.ITokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

import static com.ruoyi.common.constant.ShiroConstants.ACCESSTOKEN_CACHE;

@Slf4j
@Controller
public class ErpController extends BaseController {

    @Resource
    ITokenService iTokenService;

    @Resource
    URLConfig urlConfig;

    /**
     * 获取合同信息列表服务页面
     *
     * @return
     */
    @RequiresPermissions("contract:get_contract")
    @GetMapping("/contract/get_contract")
    public String get_contract() {
        return "system/erp/get_contract";
    }


    /**
     * 获取合同信息列表服务
     *
     * @param contract_name
     * @param contract_code
     * @return
     */
    @RequiresPermissions("contract:get_contract_list")
    @PostMapping("/contract/get_contract_list")
    @ResponseBody
    public TableDataInfo get_contract_list(@RequestParam String contract_name, @RequestParam String contract_code) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        TableDataInfo rspData = new TableDataInfo();

        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        JSONObject obj = new JSONObject();
        obj.put("page_size", pageSize);
        obj.put("page_num", pageNum);
        obj.put("contract_name", contract_name);
        obj.put("contract_code", contract_code);

        String msg = iTokenService.getList(urlConfig.getGet_contract_list(), obj.toJSONString());
        if (msg.indexOf("失败") > -1) {
            return rspData;
        }
        ContractResult.DataBean result = JSONObject.parseObject(msg, ContractResult.DataBean.class);

        rspData.setCode(0);
        rspData.setRows(result.getResult());
        rspData.setTotal(result.getRowTotal());
        return rspData;
    }

    /**
     * 获取合同物资明细信息列表服务页面
     *
     * @return
     */
    @RequiresPermissions("contract:get_material")
    @GetMapping("/contract/get_material")
    public String get_material() {
        return "system/erp/get_material";
    }

    /**
     * 获取合同物资明细信息列表服务
     *
     * @param contract_code
     * @return
     */
    @RequiresPermissions("contract:get_material_list")
    @PostMapping("/contract/get_material_list")
    @ResponseBody
    public TableDataInfo get_material_list(@RequestParam String contract_code) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        TableDataInfo rspData = new TableDataInfo();

        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        JSONObject obj = new JSONObject();
        obj.put("page_size", pageSize);
        obj.put("page_num", pageNum);
        obj.put("contract_code", contract_code);

        String msg = iTokenService.getList(urlConfig.getGet_material_list(), obj.toJSONString());
        if (msg.indexOf("失败") > -1) {
            return rspData;
        }
        MaterialResult.DataBean result = JSONObject.parseObject(msg, MaterialResult.DataBean.class);

        rspData.setCode(0);
        rspData.setRows(result.getResult());
        rspData.setTotal(result.getRowTotal());
        return rspData;
    }

    /**
     * 获取排产资料确认情况服务
     *
     * @param contract_code
     * @return
     */
    @RequiresPermissions("contract:get_schedule_confirm_list")
    @PostMapping("/contract/get_schedule_confirm_list")
    @ResponseBody
    public TableDataInfo get_schedule_confirm_list(@RequestParam String contract_code) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        TableDataInfo rspData = new TableDataInfo();

        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        JSONObject obj = new JSONObject();
        obj.put("page_size", pageSize);
        obj.put("page_num", pageNum);
        obj.put("contract_code", contract_code);

        String msg = iTokenService.getList(urlConfig.getGet_schedule_confirm_list(), obj.toJSONString());
        if (msg.indexOf("失败") > -1) {
            return rspData;
        }
        MaterialResult.DataBean result = JSONObject.parseObject(msg, MaterialResult.DataBean.class);

        rspData.setCode(0);
        rspData.setRows(result.getResult());
        rspData.setTotal(result.getRowTotal());
        return rspData;
    }

    /**
     * 获取合同排产计划列表服务（仅获取已经审批同意的排产计划）
     *
     * @param contract_code
     * @return
     */
    @RequiresPermissions("contract:get_schedule_list")
    @PostMapping("/contract/get_schedule_list")
    @ResponseBody
    public TableDataInfo get_schedule_list(@RequestParam String contract_code) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        TableDataInfo rspData = new TableDataInfo();

        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        JSONObject obj = new JSONObject();
        obj.put("page_size", pageSize);
        obj.put("page_num", pageNum);
        obj.put("contract_code", contract_code);

        String msg = iTokenService.getList(urlConfig.getGet_schedule_confirm_list(), obj.toJSONString());
        if (msg.indexOf("失败") > -1) {
            return rspData;
        }
        MaterialResult.DataBean result = JSONObject.parseObject(msg, MaterialResult.DataBean.class);

        rspData.setCode(0);
        rspData.setRows(result.getResult());
        rspData.setTotal(result.getRowTotal());
        return rspData;
    }

    /**
     * 排产资料确认服务
     *
     * @param updateSchedule
     * @return
     */
    @RequiresPermissions("contract:update_schedule_confirm")
    @PostMapping("/contract/update_schedule_confirm")
    @ResponseBody
    public String update_schedule_confirm(UpdateSchedule updateSchedule) {

        //TODO
        String[] array = new String[]{"TEST104941", "TEST104942"};
        JSONObject obj = new JSONObject();
        obj.put("confirmation_ids", array);

        String msg = iTokenService.getList(urlConfig.getUpdate_schedule_confirm(), obj.toJSONString());
        return msg;
    }

    public static void main(String[] args) {
        //TODO
//        String [] array = new String[]{"TEST104941","TEST104942"};
//        JSONObject obj = new JSONObject();
//        obj.put("confirmation_ids", array);

        JSONObject obj = new JSONObject();
        obj.put("page_size", 10);
        obj.put("page_num", 1);
        obj.put("contract_code", "HTCS-101101");

        System.out.println(obj.toJSONString());

        String key = "testKey";
        String result2 = HttpRequest.post("http://61.163.210.202:4170/api/WZGYL/scp-supplier-erp/contract/get_schedule_list")
                .header("Content-Type", "application/json")
                .header("sign", Pwd.getSign(key, obj.toJSONString()))
                .header("access_token", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJURVNUMTA0OTQiLCJpc3MiOiJTQ1AiLCJpYXQiOjE1ODY3MDY3ODUsImV4cCI6MTU4Njc5MzE4NX0.35G7Zq5Ik-34Y6qy9eCf43w3DMqXasOakRWw8YznVRF7lkQIv2y57OFIy__HnV6702EcsfPEpeNcOkanhz1WYQ")
                .body(obj.toJSONString()).execute().body();
        log.info("第三方返回结果：{}", result2);
    }
}
